<?php
//$Id: comments.php 9 2009-04-26 06:35:12Z bole.Chen $
uses('controller');

class commentsController extends Controller {

    function __construct() {
        parent::__construct();
    }

    /*
    4.没有找到相应的数据
    8.数据不完整
    */
    function comments() {
        $data['iid']    = intval($this->request['iid']);
        $data['act']    = trim($this->request['act']);

        if ($data['act'] == 'new'){
            $result = $this->newComment($data); //新发表
        } elseif (intval($data['iid'])) {
            $result = $this->commentList($data); //评论列表
        } else {
            $result = $this->commentListAll($data); //所有评论
            //$result['code']['comments'] = 4;
        }

        return $result;
    }

    function trackBack() {
        $data['iid'] = intval($this->request['iid']);

        if ($data['iid']) {
            if ($this->request['url']) {
                $data['title']      = trim($this->request['title']);
                $data['excerpt']    = $this->cn_substr(trim($this->request['excerpt']), 80);
                $data['url']        = trim($this->request['url']);
                $data['blog_name']  = trim($this->request['blog_name']);
                $data['ip']         = $_SERVER['REMOTE_ADDR'];

                $this->logger(print_r($data, true), 'trackback.log');

                //判断是否spam
                //依据：判断来源url中是否包括 loopo 字串
                $tmp = @file_get_contents($data['url']);
                if ($tmp && strpos($tmp, 'loopo')) {
                    $result = $this->model->newComment($data);
                    $result['code']['trackBack'] =& $result['code']['newComment'];
                } else {
                    $result['code']['trackBack'] = 64;
                }
            } else {
                $result['code']['trackBack'] = 8;
            }
        } else {
            $result['code']['trackBack'] = 64;
        }

        return $result;
    }

    /*
    2.更新错误
    4.内容重复
    8.内容为空
    16.姓名为空
    32.邮件为空
    64.非法请求
    128.说的太少了
    256.spam
    */
    function newComment($data=null) {
        $this->debugmsg("commentsController::newComment");

        $data['iid']        = intval($this->request['iid']);

        $data['body']       = $this->_preFormat($this->request['body'], true);
        $data['uname']      = $this->session['uname'] ? $this->session['uname'] : $this->_preFormat($this->request['uname']);
        $data['umail']      = $this->session['umail'] ? $this->session['umail'] : $this->_preFormat($this->request['umail']);

        //验证码
        if (!$this->session['login'] && (!$this->session['rand_code'] || !$this->request['rand_code'] || ($this->request['rand_code'] != $this->session['rand_code'])) ) $result['code']['newComment'] = 256;

        //判断留言中是否包含中文
        $f = 0;
        if (preg_match("/^([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}/",$str) || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){1}$/",$str) || preg_match("/([".chr(228)."-".chr(233)."]{1}[".chr(128)."-".chr(191)."]{1}[".chr(128)."-".chr(191)."]{1}){2,}/", $data['body'])) $f = 1;
        if ($f==0) $result['code']['newComment'] = 256;

        if (strlen($data['body']) < 10) $result['code']['newComment'] = 128;
        if (!$data['body']) $result['code']['newComment'] = 8;

        //不再判断email v2.0
        //if (!$this->session['login'] && !preg_match($this->regexp['email'], $data['umail']))  $result['code']['newComment'] = 32;

        if (!$this->session['login'] && !$data['uname'])  $result['code']['newComment'] = 16;

        if (!$result['code']) {
            $this->session['uname'] = $data['uname'];
            $this->session['umail'] = $data['umail'];

            if ($this->session['login']) {
                $data['uid']    = $this->session['id'];
                $data['uname']  = $this->session['username'];
                $data['umail']  = $this->session['email'];
            }

            //管理员名称
            $data['isadmin']    = $this->session['admin'];
            $data['ip']         = $_SERVER['REMOTE_ADDR'];

            $result = $this->model->newComment($data);
            $result['code']['comments'] = $result['code']['newComment'];
        }

        uses('cache');
        cache_del('commentListAll', 'comments');
        cache_del($data['iid'], 'items'); //单个商品 cache

        $result['data'] = $data;

        return $result;
    }

    function ajaxNewComment($var) {
        parse_str($var, $this->post);
        $this->request = $this->post = array_stripslashes($this->post);
        $this->request = array_merge($this->request, $this->post);
        $result = $this->newComment();

        //print_r($result);exit;
        $rr =& $this->ajax->add_node('result');
        $rr->set_data($result['code']['newComment']);

        if($result['code']['newComment'] == 1) {
            $node =& $this->ajax->add_node('cdata');

            $uname =& $node->add_node('uname');
            $uname->set_data($result['data']['uname']);

            $body =& $node->add_node('body');
            $body->set_data(nl2br($result['data']['body']));

            $date =& $node->add_node('updated');
            $date->set_data(date('y/m/d H:i'));
        }
    }

    //话题列表
    function commentList($data) {
        $data['sort']       = 'updated';
        $data['sortorder']  = 'DESC';
        $data['pagesize']   = $data['pagesize'] ? intval($data['pagesize']) : 20;

        if ($data['remove'] = intval($this->get['remove'])) {
            $this->needAdmin();
            $this->model->delComment($data);
        }

        $result = $this->model->commentsList($data);
        $result['code']['comments'] = $result['code']['commentList'];

        if ($this->session['uname']) $data['uname'] = $this->session['uname'];
        if ($this->session['umail']) $data['umail'] = $this->session['umail'];

        $result['data'] = $data;

        return $result;
    }

    //用户的评论
    function commentListByUser($uid=null) {
        $this->needLogin();
        $data['uid'] = $uid ? $uid : $this->session['id'];

        return $this->commentList($data);
    }

    //所有评论
    function commentListAll() {
        $data['iid'] = 'all';
        $data['pagesize'] = 50;

        uses('cache');
        if ($this->get['p']>1 || !$result = cache_get('commentListAll', 'comments')) {
            $result = $this->commentList($data);
            if ($this->get['p']==1) cache_set('commentListAll', $result, 'comments');
        }
        return $result;
    }

    //内容过滤
    function _preFormat($content, $allowhtml=false) {
        return $allowhtml ? trim(strip_tags($content, '<a><b><i><u>')) : trim(strip_tags($content));
    }

    //%3 for utf8
    function cn_substr($str,$strlen=10,$other=false) {
        $strlen *= 3;
        for($i=0;$i<$strlen;$i++)
          if(ord(substr($str,$i,1))>0xa0) $j++;
        if($j%3!=0) $strlen++;
        $rstr=substr($str,0,$strlen);
        if ((strlen($str)>$strlen) && $other) {$rstr.='……';}
        return $rstr;
    }
}
?>
